package org.dean.webapps.projectname.utils;

import org.apache.commons.lang.StringUtils;
import org.dean.webapps.projectname.constants.URLConstants;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by IntelliJ IDEA.
 * User: dean
 * Date: 8/23/11
 * Time: 1:13 PM
 * To change this template use File | Settings | File Templates.
 */
public class URLUtils {
    private static String serverContextPath = "";

    public static String getVelocityRedirectURL(String url) {
        String encodedURL = (!StringUtils.isEmpty(url)) ? url : "/";
        return URLConstants.REDIRECT_PREFIX + encodedURL;
    }

    public static String getServerContextPath() {
        return serverContextPath;
    }

    public static void setServerContextPath(HttpServletRequest request) {
        URLUtils.serverContextPath = request.getContextPath();
    }

    public static String getUrl(HttpServletRequest request, String originalUrl){
        URLUtils.setServerContextPath(request);
        return getUrl(originalUrl);
    }

    public static String getUrl(String originalUrl){
        return getServerContextPath() + originalUrl;
    }
}
